<?php

namespace admin {
    
    use C\lib\Db;
    use C\lib\Session;
    use C\mod\worker;

    class ctr extends \C\ctr {

        public function __construct() {

            Db::connect();
            Session::start();
            
        }

        public function render($path, $data = []) {

            $mflag = ctr::canMonitor();
            $fflag = \admin\finances::canReportFinance(); // 报表》财务
            $cflag = \admin\finances::canAdministrative(); // 行政
            $aflag = \admin\finances::canAuth(); // 权限
            $pflag = ctr::canReportPrepaidRecord(); // 充值记录
            $sflag = ctr::canStatisticalTable(); // 充值记录
//            $rflag = ctr::canResetPassword(); // 重置密码
//            $header = $this->loadView('admin/header', true);
            $header = $this->loadView('admin/header',[
                        'mflag' => $mflag,
                        'fflag' => $fflag,
                        'cflag' => $cflag,
                        'aflag' => $aflag,
                        'pflag' => $pflag,
                        'sflag' => $sflag
//                        'rflag' => $rflag,
            ] , true);

            $data = array_merge($data, [
                'header' => $header,
            ]);
            $this->loadView('admin/'.$path, $data);

        }

        public function getLogWorker() {
            if(isset($_SESSION['worker.id'])) {
                if($worker = worker::get(['id'=>$_SESSION['worker.id']]))
                    return $worker;
            }
            header('Location: '.SITE_PATH.'admin/user');
            return false;
        }

        // 是否具有 监控菜单 使用权限
        protected function canMonitor() {
            if($worker = $this->getLogWorker()) {

                $stmt = Db::prepare('select id from cd_auth where name="' . strtr(__METHOD__, '\\', '/') . '" and find_in_set(' . $worker->id . ', worker_ids)');

                if(($stmt->execute()) && $stmt->rowCount() > 0) {
                    return true;
                }else
                    return false;
            } else {
                return false;
            }
        }

        // 是否具有 报表->财务菜单 使用权限
        protected function canReportFinance() {
            if($worker = $this->getLogWorker()) {

                $stmt = Db::prepare('select id from cd_auth where name="' . strtr(__METHOD__, '\\', '/') . '" and find_in_set(' . $worker->id . ', worker_ids)');

                if(($stmt->execute()) && $stmt->rowCount() > 0) {
                    return true;
                }else
                    return false;
            } else {
                return false;
            }
        }

        // 是否具有 报表->充值记录 使用权限
        protected function canReportPrepaidRecord(){
            if($worker = $this->getLogWorker()) {

                $stmt = Db::prepare('select id from cd_auth where name="' . strtr(__METHOD__, '\\', '/') . '" and find_in_set(' . $worker->id . ', worker_ids)');

                if(($stmt->execute()) && $stmt->rowCount() > 0) {
                    return true;
                }else
                    return false;
            } else {
                return false;
            }
        }

        // 是否具有 行政菜单 使用权限
        protected function canAdministrative() {
            if($worker = $this->getLogWorker()) {

                $stmt = Db::prepare('select id from cd_auth where name="' . strtr(__METHOD__, '\\', '/') . '" and find_in_set(' . $worker->id . ', worker_ids)');

                if(($stmt->execute()) && $stmt->rowCount() > 0) {
                    return true;
                }else
                    return false;
            } else {
                return false;
            }
        }


        // 是否具有 权限菜单 使用权限
        protected function canAuth() {
            if($worker = $this->getLogWorker()) {

                $stmt = Db::prepare('select id from cd_auth where name="' . strtr(__METHOD__, '\\', '/') . '" and find_in_set(' . $worker->id . ', worker_ids)');

                if(($stmt->execute()) && $stmt->rowCount() > 0) {
                    return true;
                }else
                    return false;
            } else {
                return false;
            }
        }

        // 是否具有 统计报表 使用权限
        protected function canStatisticalTable() {
            if($worker = $this->getLogWorker()) {
                $stmt = Db::prepare('select id from cd_auth where name="' . strtr(__METHOD__, '\\', '/') . '" and find_in_set(' . $worker->id . ', worker_ids)');

                if(($stmt->execute()) && $stmt->rowCount() > 0) {
                    return true;
                }else
                    return false;
            } else {
                return false;
            }
        }

        // 是否具有 重置密码 使用权限
        protected function canResetPassword() {
            if($worker = $this->getLogWorker()) {
                $stmt = Db::prepare('select id from cd_auth where name="' . strtr(__METHOD__, '\\', '/') . '" and find_in_set(' . $worker->id . ', worker_ids)');

                if(($stmt->execute()) && $stmt->rowCount() > 0) {
                    return true;
                }else
                    return false;
            } else {
                return false;
            }
        }
    }

}

